package com.dji.swing.controller;

import com.dji.bean.Autowired;
import com.dji.bean.Compoment;
import com.dji.bean.InitializingBean;
import com.dji.swing.model.BrowEventListener;
import com.dji.swing.view.BrowserItemView;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.dom.By;
import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.dom.DOMElement;
import com.teamdev.jxbrowser.chromium.dom.events.DOMEventType;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;

/**
 * 给浏览器注册事件
 * 
 * @author xinlin.wang
 *
 */
@Compoment
public class BrowController implements InitializingBean {
	@Autowired
	private BrowserItemView browserItemView;
	@Autowired("com.dji.swing.model.BrowEventListener")
	private BrowEventListener domListener;

	public void afterProPertiesSet() {
		browserItemView.getBrowser().addLoadListener(new LoadAdapter() {
			@Override
			public void onFinishLoadingFrame(FinishLoadingEvent event) {
				Browser browser = browserItemView.getBrowser();
				String currentURL = browser.getURL();
				DOMDocument document = browser.getDocument();
				DOMElement documentElement = document.getDocumentElement().findElement(By.id("fullScreen"));
				if (documentElement != null) {
					documentElement.addEventListener(DOMEventType.OnClick, domListener, false);
				}
				documentElement = document.getDocumentElement().findElement(By.id("exit"));
				if (documentElement != null) {
					documentElement.addEventListener(DOMEventType.OnClick, domListener, false);
				}
				if (currentURL.endsWith("login.html")) {
					documentElement = document.getDocumentElement().findElement(By.id("login"));
					if (documentElement != null) {
						documentElement.addEventListener(DOMEventType.OnClick, domListener, false);
					}
				} else if (currentURL.endsWith("main.html")) {
					documentElement = document.getDocumentElement().findElement(By.id("mainPage"));
					if (documentElement != null) {
						documentElement.addEventListener(DOMEventType.OnClick, domListener, false);
					}
					documentElement = document.getDocumentElement().findElement(By.id("img"));
					if (documentElement != null) {
						documentElement.addEventListener(DOMEventType.OnClick, domListener, false);
					}
				}
			}
		});

	}
}
